package com.qhhc.controller;


import com.qhhc.service.VoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/voice")
@CrossOrigin(origins = "*") // 允许跨域请求
public class VoiceController {

    @Autowired
    private VoiceService voiceService;

    @PostMapping("/transcribe")
    public ResponseEntity<String> transcribeAudio(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "useLocalStorage", defaultValue = "true") String useLocalStorageStr,
            @RequestParam(value = "isStreaming", defaultValue = "false") String isStreamingStr,
            @RequestParam(value = "isFinal", defaultValue = "false") String isFinalStr) {
        try {
            // 将字符串参数转换为布尔值
            boolean useLocalStorage = parseStringToBoolean(useLocalStorageStr, true);
            boolean isStreaming = parseStringToBoolean(isStreamingStr, false);
            boolean isFinal = parseStringToBoolean(isFinalStr, false);

            System.out.println("使用本地存储: " + useLocalStorage +
                    ", 流式处理: " + isStreaming +
                    ", 是否最终片段: " + isFinal);

            String result;
            if (isStreaming) {
                // 使用流式处理
                result = voiceService.processStreamingAudio(file, useLocalStorage, isFinal);
            } else if (useLocalStorage) {
                // 使用本地API处理
                result = voiceService.processAudioFromLocal(file);
            } else {
                // 使用OSS存储和阿里云处理
                result = voiceService.processAudio(file);
            }
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body("{\"error\": \"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 处理流式音频数据
     *
     * @param audioFile       音频文件
     * @param useLocalStorage 是否使用本地存储
     * @param isFinal         是否为最后一个片段
     * @return 识别结果
     */
    @PostMapping("/streaming")
    public ResponseEntity<String> processStreamingAudio(
            @RequestParam("audio") MultipartFile audioFile,
            @RequestParam(value = "useLocalStorage", defaultValue = "true") boolean useLocalStorage,
            @RequestParam(value = "final", defaultValue = "false") boolean isFinal) {

        try {
            if (audioFile.isEmpty()) {
                return ResponseEntity.badRequest().body("{\"error\": \"音频文件为空\"}");
            }

            System.out.println("接收到" + (isFinal ? "最终" : "流式") + "音频片段，大小: " + audioFile.getSize() + " 字节");

            // 使用服务处理音频
            String result = voiceService.processStreamingAudio(audioFile, useLocalStorage, isFinal);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("{\"error\": \"处理音频失败: " + e.getMessage() + "\"}");
        }
    }

    /**
     * 将字符串参数转换为布尔值
     * 
     * @param value        字符串值
     * @param defaultValue 默认值
     * @return 布尔值结果
     */
    private boolean parseStringToBoolean(String value, boolean defaultValue) {
        if (value == null || value.isEmpty()) {
            return defaultValue;
        }
        return "true".equalsIgnoreCase(value) ||
                "1".equals(value) ||
                "yes".equalsIgnoreCase(value);
    }
}